﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using Sinacor.Infra.UI.Controls.SmartClient;
using Sinacor.Infra.UI.Common;
using System.Windows.Threading;

namespace Sinacor.Infra.UI.Desktop.SmartClient
{
    public class DesktopVirtual : DispatcherObject, IDisposable
    {
        private static Mutex mtx = new Mutex();
        private static DesktopVirtual _desktopVirtual;

        private WindowManager _windowManager;
        private Dictionary<string, List<SinacorWindow>> _dctWindowByDesktop;

        protected DesktopVirtual()
        {
            _dctWindowByDesktop = new Dictionary<string, List<SinacorWindow>>();

            _windowManager = WindowManager.GetInstance();
            _windowManager.OnLoadWindow += new WindowManager.OnLoadWindowDelegate(_windowManager_OnLoadWindow);
            _windowManager.OnUnloadWindow += new WindowManager.OnUnloadWindowDelegate(_windowManager_OnUnloadWindow);
        }

        public static DesktopVirtual GetInstance()
        {
            mtx.WaitOne();
            if (_desktopVirtual == null)
                _desktopVirtual = new DesktopVirtual();
            mtx.ReleaseMutex();

            return _desktopVirtual;
        }

        public void ChangeDesktopVirtual(string tokenID)
        {
            if (_dctWindowByDesktop != null)
            {
                lock (_dctWindowByDesktop)
                {
                    if (!_dctWindowByDesktop.ContainsKey(tokenID))
                    {
                        List<SinacorWindow> lst = new List<SinacorWindow>();
                        _dctWindowByDesktop.Add(tokenID, lst);
                    }
                }
            }

            //Alteração para fechar todas as telas ao invém de deixa-las invisiveis.
            _windowManager.CloseWindows();

            //_windowManager.NotifyDesktopVirtualChanged(tokenID);
        }

        void _windowManager_OnUnloadWindow(SinacorWindow sinacorwindow)
        {
            if (_dctWindowByDesktop != null)
            {
                lock (_dctWindowByDesktop)
                {
                    SessionInformation si = sinacorwindow.GetSessionInformation();
                    _dctWindowByDesktop[si.TokenId].Remove(sinacorwindow);
                }
            }
        }

        void _windowManager_OnLoadWindow(SinacorWindow sinacorwindow)
        {
            if (_dctWindowByDesktop != null)
            {
                lock (_dctWindowByDesktop)
                {
                    SessionInformation si = sinacorwindow.GetSessionInformation();
                    _dctWindowByDesktop[si.TokenId].Add(sinacorwindow);
                }
            }
        }

        public Dictionary<string, List<SinacorWindow>> GetDesktopVirtual()
        {
            return _dctWindowByDesktop;
        }

        public List<SinacorWindow> GetDesktopVirtual(string tokenId)
        {
            List<SinacorWindow> windows = new List<SinacorWindow>();

            if (_dctWindowByDesktop.ContainsKey(tokenId))
                windows = _dctWindowByDesktop[tokenId];

            return windows;
        }

        #region IDisposable Members

        public void Dispose()
        {
            if (_dctWindowByDesktop != null)
            {
                lock (_dctWindowByDesktop)
                {
                    _dctWindowByDesktop.Clear();
                }
            }
        }

        #endregion
    }
}
